package com.dunqi.gpm.shunqing.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dunqi.gpm.shunqing.R;
import com.dunqi.gpm.shunqing.common.api.ApiFactory;
import com.dunqi.gpm.shunqing.common.api.RxSchedulers;
import com.dunqi.gpm.shunqing.common.base.BaseAct;
import com.dunqi.gpm.shunqing.common.base.RecyclerAdapter;
import com.dunqi.gpm.shunqing.common.util.SwipeUtil;
import com.dunqi.gpm.shunqing.common.view.ComTitleBar;
import com.dunqi.gpm.shunqing.home.adapter.SupervisoryManagementAdapter;
import com.dunqi.gpm.shunqing.home.bean.Page;
import com.dunqi.gpm.shunqing.home.bean.ProjectTimeline;
import com.dunqi.gpm.shunqing.home.widget.EmptyView;
import com.dunqi.gpm.shunqing.my.util.UserUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.functions.Consumer;

public class SupervisoryManagementActivity extends BaseAct implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener {


    private static final String DATA = "data";
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    @BindView(R.id.title_bar)
    ComTitleBar titleBar;
    ProjectTimeline item;
    private RecyclerAdapter adapter;
    List<Management> data = new ArrayList<>();

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {
        ApiFactory.getInstance()
                .querySuperviseListByProgress(UserUtil.userFromRespOrg() ? null : item.getProPlanProgressId()
                        , page,
                        UserUtil.userFromRespOrg() ? true : null)
                .compose(RxSchedulers.<Page<Management>>io_main())
                .subscribe(new Consumer<Page<Management>>() {
                    @Override
                    public void accept(Page<Management> pages) throws Exception {
                        SwipeUtil.loadCompleted(swipeRefresh);
                        ArrayList<Management> list = pages.getList();
                        if (pages.isHasNextPage()) {
                            adapter.loadMoreComplete();
                        } else {
                            adapter.loadMoreEnd(true);
                        }
                        if (page == 1) {
                            adapter.replaceData(list);
                        } else {
                            adapter.addData(list);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        adapter.loadMoreFail();
                        t("请求失败");
                        SwipeUtil.loadCompleted(swipeRefresh);
                    }
                });
    }

    @Override
    protected void initUiAndListener() {
        item = (ProjectTimeline) getIntent().getSerializableExtra(DATA);

        SwipeUtil.init(swipeRefresh);
        recycler.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SupervisoryManagementAdapter(data);
        recycler.setAdapter(adapter);
        adapter.setEmptyView(new EmptyView(this));
        swipeRefresh.setOnRefreshListener(this);
        adapter.setOnLoadMoreListener(this, recycler);
        adapter.setOnItemClickListener(this);

        if (UserUtil.userFromNdrcOrg()) {
            titleBar.setRightText("新增督办");
        } else {
            titleBar.setRightText("");
        }

        titleBar.setOnClickListenerOfRight(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (UserUtil.userFromNdrcOrg()) {
                    addOne();
                }
            }
        });
    }


    private void addOne() {
        EditSupervisoryManagementActivity.start(this, item, -1,"");
    }


    @Override
    public void onLoadMoreRequested() {
        net();
    }

    @Override
    public void onRefresh() {
        page = 1;
        net();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (UserUtil.userFromNdrcOrg()) {
            Management management = data.get(position);
            EditSupervisoryManagementActivity.start(this, item,
                    management.getSuperviseRecordId(),management.getSuperviseContent());
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_supervisory_management;
    }


    /**
     * 这边的接口没有一种让人赏心悦目的感觉
     * 做到后面才发现需要把整个对象传过来
     * 但是本界面只需要一个参数
     * 可能你会说我技术不行什么的
     * 但是在我公司从来没出过这种情况
     * 按部就班的写下去不会操心什么东西
     *
     * @param context
     * @param *proPlanProgressId -1表示不传
     */
    public static void start(Context context, ProjectTimeline projectTimeline) {
        Intent intent = new Intent(context, SupervisoryManagementActivity.class);
        intent.putExtra(DATA, projectTimeline);
        context.startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == EditSupervisoryManagementActivity.RESULT_CODE) {
            swipeRefresh.setRefreshing(true);
            onRefresh();
        }
    }
}
